<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <el-col :span="12">
        <div class="opration_wrap">
          <el-button type="primary" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
        </div>
      </el-col>
    </el-row>
    <div class="add_wrap">
      <div class="tit">
        <h2>新增商品规格</h2>
      </div>
      <el-form :model="addForm" ref="addForm" label-width="200px" :rules="roleRules">
        <el-row>
          <el-form-item label="医院名称" prop="institutionId">
            <el-select v-model="addForm.institutionId" filterable placeholder="请选择医院">
              <el-option v-for="item in insData" :key="item.institutionId" :label="item.institutionName"
                         :value="item.institutionId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格名称" prop="specName">
            <el-input v-model="addForm.specName"></el-input>
          </el-form-item>
          <el-form-item label="规格值(多个以英文逗号隔开)" prop="specValue">
            <!--<el-input v-model="addForm.specValue"></el-input>-->
            <el-input type="textarea" v-model="addForm.specValue" placeholder="请输入规格值"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="specNote">
            <el-input type="textarea" v-model="addForm.specNote" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-button type="primary" @click="sureClick">保 存</el-button>
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
  import * as validators from "@/utils/validators"
  import * as message from "@/constants/message"
  import Crumbs from "@/components/Crumbs"
  import Region from "@/components/Region"
  export default{
    data(){
      return {
        addForm: {
          institutionId:"",
          specName:"",
          specValue:"",
          specNote:""
        },
        insData: [],
        disabled: "",
        roleRules: {
          institutionId: [
            {required: true, message: "请选择医院", trigger: "change"}
          ],
          specName: [
            {required: true, message: "请输入规格名称", trigger: "blur"}
          ],
          specValue: [
            {required: true, message: "请输入规格值", trigger: "blur"}
          ],
          specNote: [
            { max: 151, message: '长度不超过150个字', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      Crumbs,
      Region
    },
    methods: {
      int(){
        this.getInsData();
      },
      sureClick() {
        this.$refs.addForm.validate(valid => {
          if (valid) {
            this.$confirm(message.MSG_CONFIRM_SAVE, message.TITLE_PROMPT, {})
              .then(() => {
                this.addInstitutionTrade();
              });
          }
        })
      },
      addInstitutionTrade() {
        let data = {
          institutionId: this.addForm.institutionId,
          specName: this.addForm.specName,
          specValue: this.addForm.specValue,
          specNote: this.addForm.specNote
        };
        this.$Post(this.$Config.api.goodsSpec, data)
          .then(data => {
            this.$notify.success({
              title: message.TITLE_SUCCESS,
              message: message.MSG_ACTION_SUCCESS
            });
            this.returnList();
          })
          .catch(error => {
            if (error.msg) {
              this.$notify.error({
                title: message.TITLE_ERROR,
                message: error.msg
              });
            }
          })
      },
      getInsData(){
        this.$Get(this.$Config.api.institutionName)
          .then((res) => {
            this.insData = res.data;
          })
          .catch(err => {
            console.log(err)
          })
      },
      returnList(){
        this.$router.push({path: '/good/goodsSpecManagement'})
      }
    },

    mounted(){
      this.int();
    }
  }
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope>
</style>
